<template>
  <view class="container" :style="{ backgroundColor: pageinfo.bgcolor }">
    <dp :pagecontent="pagecontent"></dp>
    <view v-if="copyright != ''" class="copyright">{{ copyright }}</view>
    <dp-tabbar :opt="opt"></dp-tabbar>
    <popmsg ref="popmsg"></popmsg>
    <loading v-if="loading"></loading>
  </view>
</template>

<script>
var app = getApp();
export default {
  data() {
    return {
      opt: {},
      loading: false,
      isload: false,
      pageinfo: [],
      pagecontent: [],
      copyright: "",
      money_list: [],
      mid: 0,
    };
  },
  onShow:function(opt){
  	var flag = uni.getStorageSync("headChange");
  	if(flag == 1){
  		uni.setStorageSync("headChange",0)
  		app.goto('/pages/my/usercenter','reLaunch')
  	}
  },
  onLoad: function (opt) {
    this.opt = app.getopts(opt);
    var mid = uni.getStorageSync("mid");
    this.mid = mid;
    if (mid) {
      this.getMoney();
      this.getdata();
    } else {
      this.getMockData();
    }
  },
  onPullDownRefresh: function (e) {
    var mid = uni.getStorageSync("mid");
    if (mid) {
      this.getMoney();
      this.getdata();
    } else {
      this.getMockData();
    }
  },
  methods: {
    getMoney() {
      app.get("Money/requestMerchantTransfer", {}, (res) => {
        this.money_list = res.data;
        if (res.data.package_list.length > 0) {
          let package_info = res.data.package_list[0].package_info;
          if (wx.canIUse("requestMerchantTransfer")) {
            wx.requestMerchantTransfer({
              mchId: this.money_list.mchId,
              appId: wx.getAccountInfoSync().miniProgram.appId,
              package: package_info,
              success: (res) => {
                // res.err_msg将在页面展示成功后返回应用时返回ok，并不代表付款成功
                console.log("success:", res);
              },
              fail: (res) => {
                console.log("fail:", res);
              },
            });
          } else {
            wx.showModal({
              content: "你的微信版本过低，请更新至最新版本。",
              showCancel: false,
            });
          }
        }
      });
    },
    getdata: function () {
      var that = this;
      that.loading = true;
      app.get("My/usercenter", {}, function (data) {
        that.loading = false;
        var pagecontent = data.pagecontent;
        that.pageinfo = data.pageinfo;
        that.pagecontent = data.pagecontent;
        that.copyright = data.copyright;
        app.globalData.vip_info = {
          vip_card_pay: data.vip_card_pay,
          vip_card_image: data.vip_card_image,
          messageNum: data.message_num || 0,
        };
        if (data.memberinfo) {
          app.globalData.memberinfo = data.memberinfo;
        }
        if (data.can_apply_level) {
          app.globalData.nextlevelinfo = data.can_apply_level;
        }
        uni.setNavigationBarTitle({
          title: data.pageinfo.title,
        });
        that.loaded();
      });
    },
    getMockData() {
      this.pagecontent = [
        {
          id: 6666,
          temp: "userinfo",
          data: {
            userinfo: {
              nickname: "未登录",
              headimg: "https://hd.hedashepin.com/static/img/touxiang.png",
            },
            orderinfo: {
              count0: 0,
              count1: 0,
              count2: 0,
              count3: 0,
              count4: 0,
            },
          },
          params: {},
        },
      ];
    },
  },
};
</script>
<style>
.dp-text {
  border-bottom: 2rpx dashed #999;
  border-radius: 16rpx 16rpx 0rpx 0rpx;
}

.dp-text text {
  font-weight: bold;
}

.dp-menu {
  border-radius: 0rpx 0rpx 16rpx 16rpx;
}
</style>
